package com.small.nacos.config.server.controller;

import small.common.core.util.StringUtil;
import com.small.nacos.api.common.ApiCommonConstants;
import com.small.nacos.api.common.ApiConstants;
import com.small.nacos.config.server.service.dump.DumpService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(ApiConstants.COMMUNICATION_CONTROLLER_PATH)
public class CommunicationController {

    private final DumpService dumpService;

    public CommunicationController(DumpService dumpService) {
        this.dumpService = dumpService;
    }

    @GetMapping("/dataChange")
    public Boolean notifyConfigInfo(HttpServletRequest request, @RequestParam("dataId") String dataId,
                                    @RequestParam("group") String group,
                                    @RequestParam(value = "tenant", required = false, defaultValue = StringUtil.EMPTY) String tenant) {
        dataId = dataId.trim();
        group = group.trim();
        String lastModified = request.getHeader(ApiCommonConstants.NOTIFY_HEADER_LAST_MODIFIED);
        long lastModifiedTs = StringUtil.isEmpty(lastModified) ? -1 : Long.parseLong(lastModified);

        dumpService.dump(dataId, group, tenant, lastModifiedTs);

        return true;
    }
}
